package com.aleaho.cftrack.activity;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.transition.Fade;
import android.transition.TransitionInflater;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.aleaho.cftrack.CFTrackApplication;
import com.aleaho.cftrack.R;
import com.aleaho.cftrack.db.gen.UserDao;
import com.aleaho.cftrack.presenter.LoginPresenter;
import com.aleaho.cftrack.utils.ViewUtil;
import com.aleaho.cftrack.view.LoginView;

import java.lang.ref.WeakReference;

import butterknife.BindView;

public class LoginActivity extends BaseActivity implements LoginView {

    @BindView(R.id.etUserName)
    EditText etUserName;

    @BindView(R.id.etPassword)
    EditText etPassword;
    @BindView(R.id.view_part)
    LinearLayout viewPart;

    @BindView(R.id.btnLogin)
    Button btnLogin;

    private LoadingDialog dialog;

    private LoginPresenter loginPresenter = null;
    private CFTrackApplication trackApp = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupWindowAnimations();
        setTitle(R.string.activity_login);
        initView();

        trackApp = (CFTrackApplication) getApplicationContext();

        loginPresenter = new LoginPresenter(this, trackApp);
    }


    @TargetApi(22)
    private void setupWindowAnimations() {
        Fade fade = new Fade();
        fade.setDuration(1000);
        getWindow().setEnterTransition(fade);
    }


    private void initView() {

        dialog = new LoadingDialog(this);
        dialog.setCanceledOnTouchOutside(false);

        etPassword.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                Rect r = new Rect();
                //获取当前界面可视部分
                LoginActivity.this.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
                //获取屏幕的高度
                int screenHeight = LoginActivity.this.getWindow().getDecorView().getRootView().getHeight();
                //此处就是用来获取键盘的高度的， 在键盘没有弹出的时候 此高度为0 键盘弹出的时候为一个正数
                int heightDifference = screenHeight - r.bottom;
                if (heightDifference > 0) {
                    viewPart.setTranslationY(-80);
                } else {
                    viewPart.setTranslationY(80);
                }
                Log.d("Keyboard Size", "Size: " + heightDifference);
            }
        });
    }

    @Override
    public int getResourcesId() {
        return R.layout.activity_login;
    }

    public void onClick(View v) {
        String username = etUserName.getText().toString();
        String password = etPassword.getText().toString();

        loginPresenter.login(username, password);
    }


    @Override
    public void turnToMain() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);

        overridePendingTransition(R.anim.in_from_right, R.anim.keep);

        finish();
    }

    @Override
    public void loginError(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}
